package net.maku.module.util.check;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

@Service
public class ContentCheckServiceJackson {

    @Value("${check-content-new.path}")
    public String path;


    @Value("${check-content-new.api-path}")
    private String apiPath;

    @Value("${check-content-new.app-key}")
    public String appKey;

    @Value("${check-content-new.app-secret}")
    private String appSecret;

    @Value("${check-content-new.skl_user_id}")
    private String sklUserId;


    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static void main(String[] args) {
        //System.out.println(checkContent("<p>黄昏，一缕轻烟从烟囱里请缨地飘出来，地面还特么的留余热，如果说像精力允沛的青年，那黄昏就像少女，清爽的风中一丝暖意。胎头望着天空，只见红彤彤的晚霞己经染红大半片天空了，形状更是千资百态：你看！一只只玩皮的老虎在奔跑，又象是正在追赶周围崩崩跳跳的小兔子。忠于还是不见了，然而却出现了一匹马，在无际的草原上奔跑着。晚霞形态千变万化，各种镜像令人赞不决口，天空的颜色也变化着：深红粉红橘红…仿佛是魔术大师的精彩魔术，让人目不假接。。省麦事件、诀地派、日厄战争、堡轮红赤、岳骨脱位，我叫敬毅民，我住在北京市海淀区海旺花园2区2号楼402室，我的电话是15250406758，我的银行卡号6230522090017843342,性别男，身份证号341203197912130133，\\\\n你好吗1+2=3?,3%5=?</p>", 1, "0"));
    }

    public String checkContent(String content, int sensitiveCheckFlag, String level) {
        try {
            URL url = new URL(path + apiPath);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("appKey", appKey);
            connection.setRequestProperty("appSecret", appSecret);
            connection.setRequestProperty("skl_user_id", sklUserId);
            connection.setRequestProperty("User-Agent", "Mozilla/5.0");
            connection.setDoOutput(true);
//            URL url = new URL("https://aicheck.xinhuaskl.com/ai-yuanshu/tilake-api/api/jzTextCheck/text/aicheck");
//            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//
//            connection.setRequestMethod("POST");
//            connection.setRequestProperty("Content-Type", "application/json");
//            connection.setRequestProperty("appKey", "M1YauiOpyIOmo988");
//            connection.setRequestProperty("appSecret", "0e8a10777f0633cad3df858fa7066295");
//            connection.setRequestProperty("skl_user_id", "UI685cadee60b2b1f42115fb38");
//            connection.setRequestProperty("User-Agent", "Mozilla/5.0");
//            connection.setDoOutput(true);

            // 使用 Jackson 构建 JSON
            ObjectNode requestBody = objectMapper.createObjectNode();
            ArrayNode contentList = objectMapper.createArrayNode();
            ObjectNode contentItem = objectMapper.createObjectNode();
            contentItem.put("key", "content");
            contentItem.put("value", content);
            contentList.add(contentItem);

            requestBody.set("checkContentList", contentList);
            requestBody.put("sensitiveCheckFlag", sensitiveCheckFlag);
            requestBody.put("level", level);

            String jsonInputString = objectMapper.writeValueAsString(requestBody);

            try (OutputStream os = connection.getOutputStream()) {
                byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
                os.write(input, 0, input.length);
            }

            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                try (BufferedReader br = new BufferedReader(
                        new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
                    StringBuilder response = new StringBuilder();
                    String responseLine;
                    while ((responseLine = br.readLine()) != null) {
                        response.append(responseLine);
                    }
                    return response.toString();
                }
            } else {
                throw new RuntimeException("HTTP error code: " + responseCode);
            }

        } catch (Exception e) {
            throw new RuntimeException("Content check failed", e);
        }
    }
}